<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="news" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd()"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 属性的组件el-tree搭建属性的结构的数据 -->
        <!-- el-tree
          default-expand-all 是否展开所有的子节点
        -->
        <el-tree default-expand-all :data="list">
          <!-- 使用作用域插槽获取组件内部的数据
           el-tree提供的默认的作用域插槽 #default="{data}" 获取el-tree组件内部的数据
          -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="chart" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                          >删除子部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 准备新增子部门或者编辑部门的弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit ? '编辑部门' : '新增子部门'"
        :visible.sync="showDialog"
        width="50%"
        @closed="hClose"
      >
      <!-- close事件，关闭弹层触发事件 -->
        <!-- v-if="showDialog" 解决表单数据不更新的问题的方式1 -->
        <!-- <det-dialog v-if="showDialog" :id="curId" @success="hSuccess" :isEdit="isEdit"  /> -->
        <det-dialog
          @close="showDialog = false"
          ref="deptRef"
          :id="curId"
          @success="hSuccess"
          :isEdit="isEdit"
          :originList="originList"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { transDataToTreeData } from "@/utils";
import { delDepartments, getDepartments } from "@/api/departments";
import detDialog from "./detDialog.vue";
export default {
  components: {
    detDialog,
  },
  data() {
    return {
      list: [], //存储部门列表
      showDialog: false, //控制弹层的显示和隐藏
      curId: "", //当前部门的id，作为子级部门的父级id
      isEdit: false,
      originList: [], //保存传给子组件的部门列表
    };
  },
  created() {
    this.loadDepartments();
  },

  methods: {
    hClose() {
      //重置表单的校验规则和数据
      console.log('关闭弹层');
      //重置子组件表单
      this.$refs.deptRef.resetForm();
    },
    hDel(id) {
      this.$confirm("您确定要删除该部门吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //确定之后实现删除
          const res = await delDepartments(id);
          console.log(res);
          if (res.code === 10000) {
            this.$message.success(res.message);
            this.loadDepartments();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    hEdit(id) {
      this.curId = id;
      this.isEdit = true; //编辑状态
      this.showDialog = true;
      // 解决数据不更新的方式2
      // 保证dom更新完成之后，再调用子组件的中的方法，重新获取数据，填充表单
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartmentsDetail();
      });
    },
    hSuccess() {
      //重新渲染列表数据，关闭弹层
      this.loadDepartments(), (this.showDialog = false);
    },
    hAdd(id) {
      // console.log(id);
      this.curId = id;
      this.showDialog = true;
      this.isEdit = false;
    },
    async loadDepartments() {
      try {
        const res = await getDepartments();
        // console.log(res);
        if (res.code !== 10000) return this.$message.error(res.message);
        this.originList = res.data.depts.map((item) => {
          const { id, code, name, pid } = item;
          return { id, code, name, pid };
        });
        this.$message.success(res.message);
        res.data.depts.shift(); //删除第一条数据,公司的名称
        //平铺数据，transDataToTreeData转换成树形数据
        this.list = transDataToTreeData(res.data.depts);
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>
